package jxtend.runtime.internal;

import static java.lang.String.format;

public final class JxtendInternalUtils {
	public static void checkArgumentNotNull(String argumentName, Object value, boolean constructorArgument) {
		if (value == null) {
			String messageFormat;
			if (constructorArgument) {
				messageFormat = "Required constructor argument '%s' is null";
			} else {
				messageFormat = "Required method argument '%s' is null";
			}

			NullPointerException e = new NullPointerException(format(messageFormat, argumentName));

			StackTraceElement[] oldStackTrace = e.getStackTrace();

			StackTraceElement[] newStackTrace = new StackTraceElement[oldStackTrace.length - 1];
			System.arraycopy(oldStackTrace, 1, newStackTrace, 0, oldStackTrace.length - 1);

			e.setStackTrace(newStackTrace);

			throw e;
		}
	}

	public static boolean equal(Object a, Object b) {
		if (a == b) {
			return true;
		}

		return a != null && a.equals(b);
	}
}
